package wiki;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.Root;

/**
 * 
 * @author mona.chandrapal
 */
public class searchIssue extends HttpServlet {

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();

		String searchstr = request.getParameter("searchstr");

		searchstr="%"+searchstr+"%";
		Root root = new Root();
		ResultSet rs = null;
		PreparedStatement pst = null;
		String sql1;
		int nissueid;

		JSONObject issue, issuedetails;
		issuedetails = new JSONObject();

		try {

			if ((searchstr != null) && (searchstr.length() > 0)) {

				root.initConnection();

				JSONObject issues = new JSONObject();

				//sql1 = "Select id, issuetitle, issuedetails, module, category, subcategory, bugid, version, createdby, createdon from issue where deleteflag=0 and published=1 and issuetitle like ? order by id desc limit 10 ";
				
				sql1 = "Select id, issuetitle, issuedetails, module, category, subcategory, bugid, version, createdby, createdon, "
				+ "MATCH (issuetitle) AGAINST (? in boolean mode) " 
				+ "AS score FROM issue where MATCH (issuetitle) AGAINST (? in boolean mode) "
				+ "and deleteflag=0 and published=1  order by score desc limit 20;";
				
						
				
				pst = root.con.prepareStatement(sql1);
				pst.setString(1, searchstr);
				pst.setString(2, searchstr);
				
				System.out.println("SQL: " +sql1 );
				rs = pst.executeQuery();
				JSONArray issuelist = new JSONArray();
				while (rs.next()) {
					issue = new JSONObject();
					issue.put("id", rs.getString("id"));
					issue.put("issuetitle", rs.getString("issuetitle"));
					issue.put("issuedetails", rs.getString("issuedetails"));
					issue.put("module", rs.getString("module"));
					issue.put("category", rs.getString("category"));
					issue.put("subcategory", rs.getString("subcategory"));
					issue.put("bugid", rs.getString("bugid"));
					issue.put("version", rs.getString("version"));
					issue.put("createdby", rs.getString("createdby"));
					issue.put("createdon", rs.getString("createdon"));
					issuelist.add(issue);
				}
				// out.println("{\"issuedetails\": [" + issuelist.toString()
				// + "]}");
				issues.put("issuedetails", issuelist);
				out.println(issues.toString());

			}

		} catch (Exception e) {
			System.out.println("Exception getting issue: " + e.toString());
		}finally{
			try {
				rs.close();
				pst.close();
				root.closeConnection();
			} catch (Exception e) {
				
			}
		}

	}

	// <editor-fold defaultstate="collapsed"
	// desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>
}
